﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTPOS_Solution.DAL;

namespace PTTPOS_Solution
{
    public partial class SetupPrinter : UserControl
    {
        public SetupPrinter()
        {
            InitializeComponent();
        }
        string status_category = "", status_print="", ID_category="";
        private void SetupPrinter_Load(object sender, EventArgs e)
        {
            //Tạo nút In bill
            Label lblbill = new Label();
            lblbill.Text = "In bill";
            lblbill.Name = "Bill";
            lblbill.BackColor = Color.DarkSlateGray;
            lblbill.AutoSize = false;
            lblbill.Visible = true;
            lblbill.Font = new Font(new FontFamily("Microsoft Sans Serif"), 11);
            lblbill.Size = new Size(87, 70);
            lblbill.Margin = new Padding(3, 3, 0, 0);
            lblbill.TextAlign = ContentAlignment.MiddleCenter;
            flowLayoutPanel1.Controls.Add(lblbill);
            lblbill.Click += CategoryList;
            //Thêm tên máy in vào bảng máy in
            InsertPrinter();
            // Tạo danh sách máy in
            GetlistPrinter();
            //Tạo danh sách ngành hàng
            GetlistCategory();
            //Tạo dữ liệu dưới listview
            SetDataLst();
            //Canh panel giữa màn hình
            panel2.Location = new Point(this.Size.Width / 2 - panel2.Size.Width / 2, this.Size.Height / 2 - panel2.Height/2);
        }

        private void InsertPrinter()
        {
            string printName = "";
            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                if (Print_DA.findNameprinterbyName(printer)=="")
                {
                    printName += printer + "|";
                }
            }
            for (int i = 0; i < printName.Split('|').Length-1; i++)
            {
                Print_DA.InsertPrinter(printName.Split('|')[i]);
            }
        }

        private void GetlistPrinter() //Lấy danh sách máy in Thêm vào flowlayout 2.
        {
            Print_DA.getlistprint();
            
            foreach (DataRow row in Print_DA.listPrinter.Rows)
            {
                Label lbl = new Label();
                lbl.Text = row["Printname"].ToString();
                lbl.Name = row[0].ToString();
                lbl.BackColor = Color.DarkSlateGray;
                lbl.AutoSize = false;
                lbl.Visible = true;
                lbl.Font = new Font(new FontFamily("Microsoft Sans Serif"), 11);
                lbl.Size = new Size(87, 70);
                lbl.Margin = new Padding(3, 3, 0, 0);
                lbl.TextAlign = ContentAlignment.MiddleCenter;
                flowLayoutPanel2.Controls.Add(lbl);
                lbl.Click += PrinterList;
            }
        }
        private void GetlistCategory()
        {
            Categories_DA.GetListCatagories();
            foreach (DataRow row in Categories_DA.ListCatagories.Rows)
            {

                Label lbl = new Label();
                lbl.Text = row[2].ToString();
                lbl.Name = row[0].ToString();
                lbl.BackColor = Color.DarkSlateGray;
                lbl.AutoSize = false;
                lbl.Visible = true;
                lbl.Font = new Font(new FontFamily("Microsoft Sans Serif"), 11);
                lbl.Size = new Size(87, 70);
                lbl.Margin = new Padding(3, 3, 0, 0);
                lbl.TextAlign = ContentAlignment.MiddleCenter;
                flowLayoutPanel1.Controls.Add(lbl);
                lbl.Click += CategoryList;
            }
        }
        private void PrinterList(object sender, EventArgs e)
        {
            if (status_category != "")
            {
                if (status_print != "" & status_print != ((Label)sender).Name)
                {
                    if (((Label)sender).BackColor == Color.DarkSlateGray)
                    {
                        ((Label)sender).BackColor = Color.Teal;
                        flowLayoutPanel2.Controls.Find(status_print, true)[0].BackColor = Color.DarkSlateGray;
                        if (status_category == "Bill")
                        {
                            Company_DA.UpdateCompany(Company_DA.getcompany().Rows[0][0].ToString(), ((Label)sender).Name);
                            listView1.Items.Clear();
                            SetDataLst();
                        }
                        else
                        {
                            Categories_DA.UpdateCategory(status_category, ((Label)sender).Name);
                            listView1.Items.Clear();
                            SetDataLst();
                        }
                    }
                    status_print = ((Label)sender).Name;
                }
                else
                {
                    status_print = ((Label)sender).Name;
                    ((Label)sender).BackColor = Color.Teal;
                    listView1.Items.Clear();
                    if (status_category == "Bill")
                    {
                        Company_DA.UpdateCompany(Company_DA.getcompany().Rows[0][0].ToString(), ((Label)sender).Name);
                        listView1.Items.Clear();
                        SetDataLst();
                    }
                    else
                    {
                        Categories_DA.UpdateCategory(status_category, ((Label)sender).Name);
                        SetDataLst();
                    }   
                }
            }
            else
                MessageBox.Show("Bạn chưa chọn ngành hàng", "Err");
        }

        private void CategoryList(object sender, EventArgs e)
        {
            if (status_category != "" & status_category != ((Label)sender).Name)
            {
                if (((Label)sender).BackColor == Color.DarkSlateGray)
                {
                    ((Label)sender).BackColor = Color.Teal;
                    flowLayoutPanel1.Controls.Find(status_category, true)[0].BackColor = Color.DarkSlateGray;
                    if(status_print!="")
                        flowLayoutPanel2.Controls.Find(status_print, true)[0].BackColor = Color.DarkSlateGray;
                }
                status_category = ((Label)sender).Name;
            }
            else
            {
                status_category = ((Label)sender).Name;
                ((Label)sender).BackColor = Color.Teal;
            }
        }

        private void SetDataLst()
        {
            Categories_DA.GetListCatagories();
            foreach (DataRow s in Categories_DA.ListCatagories.Rows)
            {
                string print = Print_DA.findNameprinterbyID(s["id_print"].ToString());
                if (print != "")
                {
                    ListViewItem item = new ListViewItem();
                    ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
                    ListViewItem.ListViewSubItem subitem2 = new ListViewItem.ListViewSubItem();
                    item.Text = s[2].ToString();
                    subitem.Text = " < - > ";
                    subitem2.Text = print;
                    item.SubItems.Add(subitem);
                    item.SubItems.Add(subitem2);
                    listView1.Items.Add(item);
                }
            }
            ListViewItem it = new ListViewItem();
            ListViewItem.ListViewSubItem subit = new ListViewItem.ListViewSubItem();
            ListViewItem.ListViewSubItem subit2 = new ListViewItem.ListViewSubItem();
            it.Text = "In bill";
            subit.Text = " < - > ";
            subit2.Text = Print_DA.findNameprinterbyID(Company_DA.getcompany().Rows[0]["printbill"].ToString());
            it.SubItems.Add(subit);
            it.SubItems.Add(subit2);
            if(subit2.Text!="")
            listView1.Items.Add(it);
        }

        private void label3_Click(object sender, EventArgs e)
        {
            Parent.Controls.Remove(this);
        }
    }
}
